package com.bosssoft.boss_exam_core.util.security.RSA;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import sun.misc.BASE64Decoder;

import java.io.*;

public class ReadPEMUtils {

    public static String loadPEM(String filename)  {
        InputStream resourceAsStream=null;
        BufferedReader br=null;
        StringBuilder stringBuilder=new StringBuilder();
        BASE64Decoder base64Decoder = new BASE64Decoder();
        InputStream in=null;
        Resource resource =new ClassPathResource("rsaFile" + File.separator + filename);
        try {
            in = resource.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("rsaFile" + File.separator + filename);
//           resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("rsaFile" + File.separator + filename);
//        resourceAsStream = ReadPEMUtils.class.getResourceAsStream("rsaFile" + File.separator + filename);

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
        try {
            String str;
            while ((str = bufferedReader.readLine()) != null) {
                if(str.charAt(0)=='-'){
                    continue;
                }
                stringBuilder.append(str);
            }
        }catch (Exception e){
            e.printStackTrace();;
        }
        return stringBuilder.toString();
    }
}
